package com.iexbuy.ihg.ui.fragment;

import android.content.Intent;
import android.graphics.Paint;
import android.net.Uri;
import android.os.Bundle;
import android.text.Html;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.RadioGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.baidu.navisdk.util.common.StringUtils;
import com.iexbuy.ihg.App;
import com.iexbuy.ihg.R;
import com.iexbuy.ihg.adapter.ShopsAdapter;
import com.iexbuy.ihg.adapter.YunGuoItemListAdapter;
import com.iexbuy.ihg.common.CommUtil;
import com.iexbuy.ihg.common.ExtraKeyConstant;
import com.iexbuy.ihg.entity.BusinessType;
import com.iexbuy.ihg.entity.BusinessTypeResponse;
import com.iexbuy.ihg.entity.CityObject;
import com.iexbuy.ihg.entity.DistrictObject;
import com.iexbuy.ihg.entity.Shop;
import com.iexbuy.ihg.entity.ShopCenter;
import com.iexbuy.ihg.entity.ShopCenterDetailResponse;
import com.iexbuy.ihg.entity.ShopCenterListResponse;
import com.iexbuy.ihg.entity.ShopCenterResponse;
import com.iexbuy.ihg.entity.ShopItemDetailObject;
import com.iexbuy.ihg.entity.ShopItemListObject;
import com.iexbuy.ihg.entity.ShopListResponse;
import com.iexbuy.ihg.entity.YunItemListObject;
import com.iexbuy.ihg.entity.YunItemListObjectResponse;
import com.iexbuy.ihg.net.ServiceHelper;
import com.iexbuy.ihg.utils.JsonUtils;
import com.iexbuy.ihg.utils.StringUtil;
import com.iexbuy.ihg.utils.ToastUtils;
import com.jingchen.pulltorefresh.PullToRefreshLayout;
import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.view.annotation.ViewInject;
import com.lidroid.xutils.view.annotation.event.OnClick;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;

public class ShopCenterDetailFragment extends BaseFragment {

	private static ShopCenterDetailFragment homeFragmentInstent;

	@ViewInject(R.id.msgListLayout)
	private PullToRefreshLayout msgListRefreshLayout;
	@ViewInject(R.id.listView)
	private ListView listView;
	@ViewInject(R.id.shopCenterZeroGouListView)
	private ListView shopCenterZeroGouListView;
	@ViewInject(R.id.shopCenterAd0GList)
	private RelativeLayout shopCenterAd0GList;

	private LinearLayout listHeaderView;
	@ViewInject(R.id.shopCenterIcon)
	private ImageView shopCenterIcon;
	@ViewInject(R.id.shopCenterName)
	private TextView shopCenterName;

	@ViewInject(R.id.imgTypeOne)
	private ImageView imgTypeOne;
	@ViewInject(R.id.imgTypeTwo)
	private ImageView imgTypeTwo;
	@ViewInject(R.id.imgTypeThree)
	private ImageView imgTypeThree;

	@ViewInject(R.id.imgTypeOneTv)
	private TextView imgTypeOneTv;
	@ViewInject(R.id.imgTypeTwoTv)
	private TextView imgTypeTwoTv;
	@ViewInject(R.id.imgTypeThreeTv)
	private TextView imgTypeThreeTv;

	@ViewInject(R.id.shopcenterCategoryOne)
	private LinearLayout shopcenterCategoryOne;
	@ViewInject(R.id.shopcenterCategoryTwo)
	private LinearLayout shopcenterCategoryTwo;
	@ViewInject(R.id.shopcenterCategoryThree)
	private LinearLayout shopcenterCategoryThree;
	@ViewInject(R.id.shopcenterCategoryFour)
	private LinearLayout shopcenterCategoryFour;


	private int pageNo = 0;
	private boolean hasNextPage;
	private ShopsAdapter shopsAdapter;
	private List<Shop> shops = new ArrayList<Shop>();
	//云购
	private List<YunItemListObject> yunItemListObjects = new ArrayList<YunItemListObject>();

	private static List<BusinessType> businessTypes = new ArrayList<BusinessType>();

	private ImageView[] imageViews =new ImageView[]{imgTypeOne,imgTypeTwo,imgTypeThree};
	private TextView[] TextViews =new TextView[]{imgTypeOneTv,imgTypeTwoTv,imgTypeThreeTv};
	private LinearLayout[] Categorys = {shopcenterCategoryOne,shopcenterCategoryTwo,shopcenterCategoryThree};
	//商城百货
	private int [] images1 = {R.drawable.ic_fushi,R.drawable.ic_xiebao,R.drawable.ic_meishi};
	private String [] name1={"服饰","鞋包","美食"};
	//家居市场
	private int [] images2 = {R.drawable.ic_chuanshangyongpin,R.drawable.ic_jiajushipin,R.drawable.ic_buyiyongpin};
	private String [] name2={"床上用品","家具饰品","布衣用品"};
	//家具建材商城
	private int [] images3 = {R.drawable.ic_cufangjiancai,R.drawable.ic_dengshizhaoming,R.drawable.ic_woshijiaju};
	private String [] name3={"厨房建材","灯饰照明","卧室家具"};
	//综合电器商城
	private int [] images4 = {R.drawable.ic_jiayongdianqi,R.drawable.ic_shoujitongxun,R.drawable.ic_diannao};
	private String [] name4={"家用电器","手机通讯","电脑"};
	//电脑数码城
	private int [] images5 = {R.drawable.ic_diannao,R.drawable.ic_shoujitongxun,R.drawable.ic_shumaxiangji};
	private String [] name5={"电脑","手机通讯","数码相机"};
	//美食广场
	private int [] images6 = {R.drawable.ic_tiandianyinpin,R.drawable.ic_xican,R.drawable.ic_zizhucan};
	private String [] name6={"甜品饮品","西餐","自助餐"};
	private static ShopCenter shopCenter;
	private static CityObject selectCityObject;
	private static DistrictObject selectDistrictObject;
	private ShopItemListObject directPayItem = null;
	private String category;
	private YunGuoItemListAdapter yunGuoItemListAdapter;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		homeFragmentInstent = this;
	}

	@Override
	protected View _onCreateView(LayoutInflater inflater, ViewGroup container,
								 Bundle savedInstanceState) {
		View rootView = inflater.inflate(R.layout.fragment_shopcenter_detail, container, false);
		ViewUtils.inject(this, rootView);
		initView(rootView);
		return rootView;
	}

	private void initView(View rootView) {
		mBaseActivity.setTitle(getResources().getString(R.string.title_shopcenter_detail));
		mBaseActivity.setTitleLayoutVisibility(true);
		mBaseActivity.setHomeActivity(false);


		listHeaderView = (LinearLayout) mBaseActivity.getLayoutInflater().from(mBaseActivity).inflate(R.layout.fragment_shopcenter_detail_header, null);
		ViewUtils.inject(this, listHeaderView);

		listView.addHeaderView(listHeaderView);

		msgListRefreshLayout.setOnRefreshListener(new PullToRefreshLayout.OnRefreshListener() {
			@Override
			public void onRefresh(PullToRefreshLayout pullToRefreshLayout) {
				refreshDate();
			}

			@Override
			public void onLoadMore(PullToRefreshLayout pullToRefreshLayout) {
				if (hasNextPage) {
					loadMore();
				} else {
					msgListRefreshLayout.refreshFinish(PullToRefreshLayout.DONE);
				}
			}
		});
		msgListRefreshLayout.autoRefresh();


	}

	private void initCategory() {
		switch (Integer.parseInt(shopCenter.getMallType())){
			case 1:
				setImage(images1 ,name1);
				break;
			case 2:
				setImage(images2 ,name2);
				break;
			case 3:
				setImage(images3 ,name3);
				break;
			case 4:
				setImage(images4 ,name4);
				break;
			case 5:
				setImage(images5 ,name5);
				break;
			case 6:
				setImage(images6 ,name6);
				break;
			default:
				break;
		}
	}

	private void setImage(int[] images , final String [] name) {
		for (int i=0;i<imageViews.length;i++){
			Log.d("aa","我进来设置图片了");
			imgTypeOne.setImageResource(R.drawable.ic_fushi);
//			imageViews[i].setScaleType(ImageView.ScaleType.FIT_XY);
			TextViews[i].setText(name[i]);
			Log.d("aa", name[i]);
		}

		for (int i =0;i<Categorys.length;i++){
			final String TypeName = name[i];
			String code=null;
			for (int j=0;j<businessTypes.size();j++){
				if(businessTypes.get(i).getName().equals(TypeName)){
					code = businessTypes.get(j).getCode();
					break;
				}
			}
			final String TypeCode = code;
			Categorys[i].setOnClickListener(new View.OnClickListener() {
				@Override
				public void onClick(View v) {
					if (TypeName!=null&&TypeCode!=null){
					ShopCenterShopSearchResultFragment.setSelectedBusinessTypeName(TypeName);
					ShopCenterShopSearchResultFragment.setSelectedBusinessTypeCode(TypeCode);
						ShopCenterShopSearchResultFragment.setShopCenter(shopCenter);
						CommUtil.openSingleFragmentActivity(mBaseActivity, ShopCenterShopSearchResultFragment.class);
					} else {
						ToastUtils.toast(mBaseActivity,"您好，正在努力完善中");
					}
				}
			});
		}
	}

	private void loadMore() {
		pageNo++;
		initListDate();
	}

	//刷新整个页面数据
	private void refreshDate() {
		initListDate();
	}

	public void getShopCentersDetailData() {
		HashMap<String, String> paramMap = new HashMap<String, String>();
		paramMap.put("id", shopCenter.getId());
		paramMap.put("latitude", CommUtil.getLatitude());
		paramMap.put("longitude", CommUtil.getLongitude());

		ServiceHelper.send(mBaseActivity, ServiceHelper.GET_SHOP_CENTERS_DETAIL, paramMap,
				new RequestCallBack<String>() {
					@Override
					public void onStart() {
					}

					@Override
					public void onCancelled() {
					}

					@Override
					public void onSuccess(ResponseInfo<String> responseInfo) {
						msgListRefreshLayout.refreshFinish(PullToRefreshLayout.SUCCEED);
						ShopCenterDetailResponse rep = JsonUtils.parseJsonStrToObject(responseInfo.result, ShopCenterDetailResponse.class);

						if (rep.getBody() != null) {

							shopCenter = rep.getBody();
							businessTypes = shopCenter.getCategory();
							initShopCenterData();
							//initCategory();
						}
					}

					@Override
					public void onFailure(HttpException error, String msg) {
						msgListRefreshLayout.refreshFinish(PullToRefreshLayout.FAIL);
					}
				});
	}

	//获取购物中心详情的人气商家列表数据
	private void initListDate() {
		HashMap<String, String> paramMap = new HashMap<String, String>();
		paramMap.put("latitude", CommUtil.getLatitude());
		paramMap.put("longitude", CommUtil.getLongitude());
		paramMap.put("pageNo", String.valueOf(pageNo));
		paramMap.put("pageSize", String.valueOf(ExtraKeyConstant.COMMON_PAGE_SIZE));
		if (shopCenter != null) {
			paramMap.put("shopCenterId", shopCenter.getId());
		}

		ServiceHelper.send(mBaseActivity, ServiceHelper.GET_SHOPLIST_BY_SHOP_CENTERS_ID, paramMap,
				new RequestCallBack<String>() {
					@Override
					public void onStart() {
						msgListRefreshLayout.refreshFinish(PullToRefreshLayout.REFRESHING);
					}

					@Override
					public void onCancelled() {
						msgListRefreshLayout.refreshFinish(PullToRefreshLayout.DONE);
					}

					@Override
					public void onSuccess(ResponseInfo<String> responseInfo) {
						msgListRefreshLayout.refreshFinish(PullToRefreshLayout.SUCCEED);
						ShopListResponse rsp = JsonUtils.parseJsonStrToObject(responseInfo.result, ShopListResponse.class);
						hasNextPage = rsp.getBody().isHasNext();
						if (pageNo == 0) {
							shops.clear();
						}
						shops.addAll(rsp.getBody().getData());

						initListView();
					}

					@Override
					public void onFailure(HttpException error, String msg) {
						msgListRefreshLayout.refreshFinish(PullToRefreshLayout.FAIL);

					}
				});

	}
	//初始化控件数据
	private void initListView(){
	/*	Collections.sort(shops, new Comparator<Shop>() {
			@Override
			public int compare(Shop shop, Shop t1) {
				return new BigDecimal(shop.getDistance()).compareTo(new BigDecimal(t1.getDistance()));
			}
		});*/
		if (shopsAdapter == null) {
			shopsAdapter = new ShopsAdapter(mBaseActivity, shops);
			listView.setAdapter(shopsAdapter);
			listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
				@Override
				public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
					ShopDetailFragment.setShop(shops.get(i - 1));//列表有头部
					CommUtil.openSingleFragmentActivity(mBaseActivity, ShopDetailFragment.class);
				}
			});
		}
		if (pageNo == 0) {
			//加载列表头部
			//initShopCenterData();
			initZeroGouListDate();
			getShopCentersDetailData();
			//加载广告
			CommUtil.initAdListData(mBaseActivity, "7", shopCenterAd0GList);

		}
		shopsAdapter.notifyDataSetChanged();
	}

	/**
	 * 加载头部数据
	 */
	private void initShopCenterData() {

		shopCenterName.setText(shopCenter.getMallName());
		if(shopCenter.getThumbnailUrl()!=null){
		App.bitmapUtils.display(shopCenterIcon, shopCenter.getThumbnailUrl());
		shopCenterIcon.setScaleType(ImageView.ScaleType.FIT_XY);
		}

	}
	private void initZeroGouListDate() {

		HashMap<String, String> paramMap = new HashMap<String, String>();
		paramMap.put("pageNo", String.valueOf(pageNo));
		paramMap.put("pageSize", String.valueOf(ExtraKeyConstant.COMMON_PAGE_SIZE));
		paramMap.put("areaCode", shopCenter.getArea());

		if (category != null) {
			paramMap.put("category", category);
		}

		ServiceHelper.send(mBaseActivity, ServiceHelper.GET_READY_OPEN_ITEMS, paramMap,
				new RequestCallBack<String>() {
					@Override
					public void onStart() {
						msgListRefreshLayout.refreshFinish(PullToRefreshLayout.REFRESHING);
					}

					@Override
					public void onCancelled() {
						msgListRefreshLayout.refreshFinish(PullToRefreshLayout.DONE);
					}

					@Override
					public void onSuccess(ResponseInfo<String> responseInfo) {
						msgListRefreshLayout.refreshFinish(PullToRefreshLayout.SUCCEED);
						YunItemListObjectResponse rsp = JsonUtils.parseJsonStrToObject(responseInfo.result, YunItemListObjectResponse.class);
						if (pageNo == 0) {
							yunItemListObjects.clear();
						}
						yunItemListObjects.addAll(rsp.getBody());

						initListZeroGouView();
					}

					@Override
					public void onFailure(HttpException error, String msg) {
						msgListRefreshLayout.refreshFinish(PullToRefreshLayout.FAIL);
						ToastUtils.toast(mBaseActivity, msg);
					}
				});
	}

	private void initListZeroGouView() {
		if (yunGuoItemListAdapter == null) {
			yunGuoItemListAdapter = new YunGuoItemListAdapter(mBaseActivity, yunItemListObjects);
			shopCenterZeroGouListView.setAdapter(yunGuoItemListAdapter);
			shopCenterZeroGouListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
				@Override
				public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
					YunGuoItemDetailFragment.setYunItemListObject(yunItemListObjects.get(i));
					YunGuoItemDetailFragment.setAreaCode(shopCenter.getArea());
					CommUtil.openSingleFragmentActivity(mBaseActivity, YunGuoItemDetailFragment.class);
				}
			});

		}

	}


	/**
	 * 场内活动
	 * @param view
	 */

	@OnClick(R.id.shopSalseLayout)
	public void shopSalseLayoutBtn(View view) {
		YunGuoDescFragment.setShopName(getResources().getString(R.string.shop_center_salse));
		YunGuoDescFragment.setWebUrl(shopCenter.getActivity());
		CommUtil.openSingleFragmentActivity(mBaseActivity, YunGuoDescFragment.class);
	}

	/**
	 * 商家品牌
	 * @param view
	 */
	@OnClick(R.id.shopBrandLayout)
	public void shopBrandLayoutBtn(View view) {
		YunGuoDescFragment.setShopName(getResources().getString(R.string.shop_center_brand));
		YunGuoDescFragment.setWebUrl(shopCenter.getBusinessBrand());
		CommUtil.openSingleFragmentActivity(mBaseActivity, YunGuoDescFragment.class);
	}
	/**
	 * 商家地图
	 * @param view
	 */
	@OnClick(R.id.shopMapLayout)
	public void shopMapLayoutBtn(View view) {
		YunGuoDescFragment.setShopName(getResources().getString(R.string.shop_center_map));
		YunGuoDescFragment.setWebUrl(shopCenter.getBusinessMap());
		CommUtil.openSingleFragmentActivity(mBaseActivity, YunGuoDescFragment.class);
	}


	/**
	 * 商家地址打开地图
	 * @param view
	 */
	@OnClick(R.id.shopAddressLayout)
	public void shopAddressLayoutBtn(View view) {
		if (StringUtil.isNotEmpty(shopCenter.getLatitude()) && StringUtil.isNotEmpty(shopCenter.getLongitude())) {
			MapFragment.setName(shopCenter.getMallName());
			MapFragment.setLatitude(shopCenter.getLatitude());
			MapFragment.setLongitude(shopCenter.getLongitude());
			CommUtil.openSingleFragmentActivity(mBaseActivity, MapFragment.class);
		}
	}
	/**
	 * 专场0购
	 * @param view
	 */
	@OnClick(R.id.shopZeroGouLayout)
	public void shopZeroGouLayoutBtn(View view) {
		ShopCenterZeroGouSearchResultFragment.setShopCenter(shopCenter);
		CommUtil.openSingleFragmentActivity(mBaseActivity, ShopCenterZeroGouSearchResultFragment.class);
	}
	/**
	 * 人气商家
	 * @param view
	 */
	@OnClick(R.id.shopListLayout)
	public void shopListLayoutBtn(View view) {
		ShopCenterShopSearchResultFragment.setShopCenter(shopCenter);
		CommUtil.openSingleFragmentActivity(mBaseActivity, ShopCenterShopSearchResultFragment.class);
	}

	/**
	 * 美食
	 * @param view
	 */
	@OnClick(R.id.shopcenterCategoryOne)
	public void shopcenter_catetBtn(View view) {
		if (businessTypes!=null) {
			ShopCenterShopSearchResultFragment.setSelectedBusinessTypeCode("6");
			ShopCenterShopSearchResultFragment.setSelectedBusinessTypeName("美食");
			ShopCenterShopSearchResultFragment.setShopCenter(shopCenter);
			CommUtil.openSingleFragmentActivity(mBaseActivity, ShopCenterShopSearchResultFragment.class);
		}
	}

	/**
	 * 休闲
	 * @param view
	 */
	@OnClick(R.id.shopcenterCategoryTwo)
	public void shopcenter_recreationBtn(View view) {
		if (businessTypes!=null) {
			ShopCenterShopSearchResultFragment.setSelectedBusinessTypeCode("8");
			ShopCenterShopSearchResultFragment.setSelectedBusinessTypeName("休闲娱乐");
			ShopCenterShopSearchResultFragment.setShopCenter(shopCenter);
			CommUtil.openSingleFragmentActivity(mBaseActivity, ShopCenterShopSearchResultFragment.class);
		}
	}

	/**
	 * 丽人
	 * @param view
	 */
	@OnClick(R.id.shopcenterCategoryThree)
	public void shopcenter_beautyBtn(View view) {
		if (businessTypes!=null) {
			ShopCenterShopSearchResultFragment.setSelectedBusinessTypeCode("5");
			ShopCenterShopSearchResultFragment.setSelectedBusinessTypeName("丽人");
			ShopCenterShopSearchResultFragment.setShopCenter(shopCenter);
			CommUtil.openSingleFragmentActivity(mBaseActivity, ShopCenterShopSearchResultFragment.class);
		}
	}
	/**
	 * 商家分类
	 * @param view
	 */
	@OnClick(R.id.shopcenterCategoryFour)
	public void shopcenter_shopcategoryBtn(View view) {
		ShopCenterShopSearchResultFragment.setSelectedBusinessTypeCode("");
		ShopCenterShopSearchResultFragment.setShopCenter(shopCenter);
		CommUtil.openSingleFragmentActivity(mBaseActivity, ShopCenterShopSearchResultFragment.class);
	}


	public static void setShopCenter(ShopCenter shopCenter) {
		ShopCenterDetailFragment.shopCenter = shopCenter;
	}

	public static void setCityShopCenter(CityObject selectCityObject) {
		ShopCenterDetailFragment.selectCityObject = selectCityObject;
	}
	public static void setDistrictShopCenter(DistrictObject selectDistrictObject) {
		ShopCenterDetailFragment.selectDistrictObject = selectDistrictObject;
	}
	public static List<BusinessType> getBusinessTypes() {
		return businessTypes;
	}

	/**
	 * @return the homeFragmentInstent
	 */
	public static ShopCenterDetailFragment getHomeFragmentInstent() {
		return homeFragmentInstent;
	}

}
